/*
 * testdominion.c
 * Random tester to play a game of dominion
 *  Created on: Feb 13, 2014
 *      Author: Charles_OSU
 */


#include "dominion.h"
#include "dominion_helpers.h"
#include <string.h>
#include <stdio.h>
#include <stdlib.h>
#include <assert.h>
#include "rngs.h"
#include <math.h>
#include "interface.h"
#include <time.h>
#include <sys/time.h>
#include <unistd.h>
#include <fcntl.h>
#include <sys/stat.h>
#include<ctype.h>



#define DEBUG 0
#define NOISY_TEST 1

// Prototypes
void setState(struct gameState *state);
int countTreasure(struct gameState *state);
int adventurerTest(int player, int card, int handPos, int choice1, int choice2, int choice3, struct gameState *state);
int smittyTest(int player, int card, int handPos, int choice1, int choice2, int choice3, struct gameState *state);
int salvagerTest(int player, int card, int handPos, int choice1, int choice2, int choice3, struct gameState *state);
int testNumberCardsHand(struct gameState* state);
int testfullDeckCount(int player, int card, struct gameState *state);
int testGreatHall(int player, int card, int handPos, int choice1, int choice2, int choice3, struct gameState *state);
void printGameSummary(struct gameState G,FILE *out, int gameNumber, int numPlayers);


int countTreasure(struct gameState *state){
    int i;
    int treasure = 0;
    for(i = 0; i < state->deckCount[0]; i++){
        if (state->deck[0][i]== gold || state->deck[0][i] == silver || state->deck[0][i] == copper){
        	treasure++;
        }
        if (treasure > 2){
            return 0;
        }
    }
    return 1;
}

int testNumberCardsHand(struct gameState* state){
	 int numerrors = 0;
	 int cards = numHandCards(state);
    //testing initial card in hand
    if(cards !=5){
        printf("number of cards was expected to be 5 in hand but were instead %d\n", cards);

    }
    return numerrors += 1;

}


int testfullDeckCount(int player, int card, struct gameState *state) {
  int count = 0;

 count = fullDeckCount( player,  card, state);

  return count;
}


int testGreatHall(int player, int card, int handPos, int choice1, int choice2, int choice3, struct gameState *state)
{

	  int numerrors = 0;
	  int testcount = 0;
	  int coin_bonus = 0;
	  int preActions  = 0;
	  int postActions = 0;

	  // Get our test count for the player and hand
	     testcount = state->handCount[0];

	     // Record how many preActions we have
	      preActions = state->numActions;

	  //check if it is the right phase
	   if (state->phase != 0)
	     {
		   numerrors += 1;
		   return numerrors;
	     }

	   //check if player has enough actions
	  	 if ( state->numActions < 1 )
	  	  {
	  	    numerrors += 1;
	  	    return numerrors;
	  	   }


	  	  //+1 Card + 1 action -1 card for discard
	  	  // Play card and check for errors
	  	     if ( cardEffect(great_hall, 0, 0, 0, state, handPos, &coin_bonus) < 0 )
	  	  	   {
	  	  	    numerrors += 1;
	  	  	    return numerrors;
	  	  	    }


	  	   // draw 1 discard 1 = net  -- This is hand count
	  	 	   if (state->handCount[0] != testcount)
	  	 	      {
	  	 	       	  numerrors = numerrors + 1;//did not match so add to error count
	  	 	       	  printf("Our current hand count (great_hall) is %d when it should be %d\n", state->handCount[0], testcount);
	  	 	        }



	  	 	// Record how many postActions we have
	  	 		      postActions = state->numActions;


	  	 	if (postActions != preActions +1)
	  	 	{
	  	 	 numerrors = numerrors + 1;//did not match so add to error count
	  	 	 printf("For great_hall our Action count was not as expected. Expected %d have %d", postActions, preActions +1);
	  	 	}


	  	  //reduce number of actions
	  		    state->numActions--;

	  		    //update coins (Treasure cards may be added with card draws)
	  		    updateCoins(state->whoseTurn, state, coin_bonus);



    return numerrors;
}



int salvagerTest(int player, int card, int handPos, int choice1, int choice2, int choice3, struct gameState *state)
{

  int numerrors = 0;
  int testcount = 0;
  int coin_bonus = 0;
  int prebuys = 0;
  int postbuys = 0;
  int coinspre = 0;
  int coinspost = 0;
  int cardcost = 0;
  int pretrashcardcnt = 0;
  int posttrashcardcnt = 0;

  prebuys =  state->numBuys;
  coinspre = state->coins;

  //check if it is the right phase
   if (state->phase != 0)
     {
	   numerrors += 1;
	   return numerrors;
     }

   //check if player has enough actions
  	 if ( state->numActions < 1 )
  	  {
  	    numerrors += 1;
  	    return numerrors;
  	   }

     // Get our test count for the player and hand
     	testcount = state->handCount[0];

     	// If we get this card in hand we will use it. We will pick one of
     	//the first 3 cards in hand to trash and get a new card

     	int opt1 = rand() % 4;

     	// record my card cost
     	cardcost = getCost( handCard(opt1, state) );

     	// Now we will check how many of these cards we have ing the deck
     	// and then check that is is gone completely (trashed) when done.
     	int handtrash = handCard(opt1, state);
     	pretrashcardcnt = 	testfullDeckCount( player,  handtrash, state);
     	printf("PreDeckCount before removal of card = %d\n", pretrashcardcnt);

     // Play card and check for errors
     if ( cardEffect(salvager, opt1, 0, 0, state, handPos, &coin_bonus) < 0 )
  	   {
  	    numerrors += 1;
  	    return numerrors;
  	    }

     // Check how many of the cards we discarded we now have in our hand
     posttrashcardcnt = testfullDeckCount( player,  handtrash, state);
     printf("PostDeckCount after removal of card = %d Card number trashed was %d\n", pretrashcardcnt, handtrash);
     if( pretrashcardcnt != posttrashcardcnt -1)
        {
       	 numerrors = numerrors + 1;
        }

     // We should have x + 1 - 1 = X buys at end. That is, we get a buy for
     // this card so +1 buy but then we use that buy to buy coins of equal value
     // which should be buys -1 so should be net effect.
     postbuys =  state->numBuys;
     if(prebuys != postbuys)
     {
    	 numerrors = numerrors + 1;
     }



     // Check that post coins are + cost of card trashed
     coinspost = state->coins;

     // So if we bought a card with y coins then our pre is x and post is
     // x + y. So x = 2 pre , y = 4 for card cost, post should be 2+4 =6
     if(coinspost != coinspre+cardcost)
     {
    	 printf("Our coin count pre was %d and post was %d and the card cost was %d\n", coinspre, coinspost, cardcost);
    	  numerrors = numerrors + 1;//did not match so add to error count
     }

     // now check if we are +3 cards
     // draw 1 discard 1 = net net  -- This is hand count
    	               if (state->handCount[0] != testcount)
    	               {
    	                	  numerrors = numerrors + 1;//did not match so add to error count
    	             	  printf("Our current hand count is %d when it should be %d\n", state->handCount[0], testcount);
    	               }

    // Need test here for trashing the card we sent through. I think they have them mixed up and so
    // they are not trahing the right card. Check flags

     //reduce number of actions
   	    state->numActions--;

   	    //update coins (Treasure cards may be added with card draws)
   	    updateCoins(state->whoseTurn, state, coin_bonus);

  	return numerrors;
}


int smittyTest(int player, int card, int handPos, int choice1, int choice2, int choice3, struct gameState *state)
{

  int numerrors = 0;
  int testcount = 0;
  int coin_bonus = 0;


  //check if it is the right phase
   if (state->phase != 0)
     {
	   numerrors += 1;
	   return numerrors;
     }

   //check if player has enough actions
  	 if ( state->numActions < 1 )
  	  {
  	    numerrors += 1;
  	    return numerrors;
  	   }

     // Get our test count for the player and hand
     	testcount = state->handCount[0];


     // Play card and check for errors
     if ( cardEffect(smithy, 0, 0, 0, state, handPos, &coin_bonus) < 0 )
  	   {
  	    numerrors += 1;
  	    return numerrors;
  	    }


     // now check if we are +3 cards
     // draw 3 discard 1 = +2  -- This is hand count
    	               if (state->handCount[0] != testcount + 2)
    	               {
    	                	  numerrors = numerrors + 1;//did not match so add to error count
    	             	  printf("Our current hand count is %d when it should be %d\n", state->handCount[0], testcount+2);
    	               }


     //reduce number of actions
   	    state->numActions--;

   	    //update coins (Treasure cards may be added with card draws)
   	    updateCoins(state->whoseTurn, state, coin_bonus);

  	return numerrors;
}


/*
 * This test will take the current playe, handPos, and passed randomly the choice of 1-3 and the game state.
 * At some point I may want to make this match the cardeffect method signature a bit better by adding the
 * bonus parameter as well so I can use this as a template for all card tests.
 */
int adventurerTest(int player, int card, int handPos, int choice1, int choice2, int choice3, struct gameState *state)
{

  int numerrors = 0;
  int testcount = 0;
  int coin_bonus = 0;

  //check if it is the right phase
   if (state->phase != 0)
     {
	   numerrors += 1;
	   return numerrors;
     }

   //check if player has enough actions
  	 if ( state->numActions < 1 )
  	  {
  	    numerrors += 1;
  	    return numerrors;
  	   }


	   if (countTreasure(state) == 1){
	           // Skip test because not enough treasure
	        }
	         else {


	  	  	  // Get our test count for the player and hand
	   	  	  testcount = state->handCount[0];

	   	  	  // call the cardeffect and see what the new count will be
	   	  	  // This will be the expected that we compare after the below test and it should equal result
	   	  	  //expected = cardEffect(card, choice1, choice2, choice3, state, handPos, 0);
	   	   if ( cardEffect(adventurer, 0, 0, 0, state, handPos, &coin_bonus) < 0 )
	   	 	      {
	   		   	   numerrors += 1;
	   			   return numerrors;
	   	 	      }
	              // cardEffect(adventurer, 0, 0, 0, state, handPos, 0);

	               // Show me the card drawn
	           //    cardDrawn = gsPre->hand[players][gsPre->handCount[players]-1];//top card of hand is most recently drawn card.
	             //         printf("cardDrawn was %d\n", cardDrawn);

	               // draw 2 discard 1 = +1  -- This is hand count
	               if (state->handCount[0] != testcount + 1)
	               {
	                	  numerrors = numerrors + 1;//did not match so add to error count
	             	  printf("Our current hand count is %d when it should be %d\n", state->handCount[0], testcount+1);
	               }


	               // Check that we should have 2 treasure cards
	               int bob;
	               for(bob=1; bob<3;  bob++)
	               {
	            	   if (state->hand[0][state->handCount[0]-bob] != copper && state->hand[0][state->handCount[0]-bob] != silver && state->hand[0][state->handCount[0]-bob] != gold)
	            	  	               {
	            	  	                   printf("Card %d drawn and left in deck should have been treasure and was not\n", bob);
	            	  	                   numerrors = numerrors + 1;//did not draw treasure

	            	  	               }
	               }

	               // Check that the last card that the player played was the adventurer card
	               // Because as we can see from the code for adventurer the code does not call
	               //  discardCard(handPos, currentPlayer, state, 0);
	               if (state->playedCards[0] != adventurer)
	               {
	            	   // the last card played was not the adventurer so add to error count
	             	  numerrors = numerrors + 1;
	                   printf("The card played was %d, but should have been %d\n", state->playedCards[0], adventurer);
	               }

  }


	   if ( cardEffect(card, choice1, choice2, choice3, state, handPos, &coin_bonus) < 0 )
	      {
	        return -1;
	      }

	    //reduce number of actions
	    state->numActions--;

	    //update coins (Treasure cards may be added with card draws)
	    updateCoins(state->whoseTurn, state, coin_bonus);

  // Return 0 if no errors otherwise return numerrors count
  return numerrors;
}

// This function looks like it has been passed round all over the class. Not sure where it originated
// from but I borrowed it from allent
void printGameSummary(struct gameState gsPre,FILE *out, int gameNumber, int numPlayers)
{
        int i, j;
        fprintf(out,"\n\n");
        fprintf(out,"Game %d over:\n",gameNumber);

    // print scores
    for(i=0;i<27;i++){
                fprintf(out,"Supply Count for [%d]: %d\n",i,gsPre.supplyCount[i]);
    }

  for(i=0;i<numPlayers;i++)
  {
        fprintf(out,"Stats for Player %d\n", i);
    fprintf(out,"Score: %d\n", scoreFor(i, &gsPre));
    fprintf(out,"Deck: ");
                for(j=0;j<gsPre.deckCount[i];j++){
                        fprintf(out,"%d, ",gsPre.deck[i][j]);
        }
        fprintf(out,"\n");
            fprintf(out,"Player %d's hand: ",i);
                for(j=0;j<gsPre.handCount[i];j++){
                        fprintf(out,"%d, ",gsPre.hand[i][j]);
                }
        fprintf(out,"\n");

        fprintf(out,"Player %d's discard: ",i);
                for(j=0;j<gsPre.discardCount[i];j++){
                        fprintf(out,"%d, ",gsPre.discard[i][j]);
                }
        fprintf(out,"\n");

  fprintf(out,"Player %d's card summary: ",i);
    for(j=0;j<gsPre.discardCount[i];j++){
      fprintf(out,"%d, ",gsPre.discard[i][j]);
    }
  fprintf(out,"\n\n");

        }
}

// sets a c;ean state and fixes some bugs that will kill the game if not compensated for
void setState(struct gameState *state){
  int i;

 // state->numPlayers = 4;
  state->numPlayers = floor(Random() * (MAX_PLAYERS -1)) + 2; // Will generate 2,3, or 4 players
  for (i = adventurer; i <= treasure_map; i++){
    state->supplyCount[i] = 10;
    state->embargoTokens[i] = 0;
  }
  state->playedCardCount = 0;
  state->outpostPlayed = 0;
  state->outpostTurn = 0;
  state->whoseTurn = 0;
  state->coins = 0;
  state->phase = 0;
  state->numActions = 1; // we always have one action to start
   state->numBuys = 1; // always start with one buy
  for (i = 0; i< state->numPlayers; i++){
    state->handCount[i] = 0;
    state->deckCount[i] = 0;
    state->discardCount[i] = 0;
  }

}



int main (int argc, char** argv) {

	int x;
	int testNums = 10; // number of tests to run
	int numErrors = 0; // counter for number of errors found
	struct gameState *gsPre = newGame();
	int pp;

	// now seed the game
	  SelectStream(3);
	  /* ---------------------------------------------------------------
	   * Use this function to set the state of the current random number
	   * generator stream according to the following conventions:
	   *    if x > 0 then x is the state (unless too large)
	   *    if x < 0 then the state is obtained from the system clock
	   *    if x = 0 then the state is to be supplied interactively
	   *    So I will set set to -1 to let clock generate seed
	   * ---------------------------------------------------------------
	   */
	  PutSeed(-1);

	  // Sets clean state each time
	  setState(gsPre);

	  // Setup my file constructor
	    char filename[] = "gameresults.out";
	    FILE *fp;
	  	fp = fopen(filename, "w");



	  	 fprintf(fp, "Testing adventurer start\n");

	  printf("Testing adventurer start\n");

	  for (x = 0; x < testNums; x++) {

    // Set the kingdom cards to nothing at first.
		int k[10] = {};

		  SelectStream(3);
		  PutSeed(-1);

	  fprintf(fp, "Starting game %d.\n\n", x);
	  printf ("Starting game %d.\n\n", x);

	  // Call from interface selectKingdomCards to get cards
	  selectKingdomCards(-1, k);


	  // Call to initialize game
	  initializeGame(gsPre->numPlayers, k, -1, gsPre);

	  // Check our hand counts to be sure they are 5
	  int yo;
	  for(yo = 0; yo < gsPre->numPlayers; yo++){
		  numErrors += testNumberCardsHand(gsPre);
	  }


	  // Call to print my supply
	//  printSupply(gsPre);


	  int money = 0;
	  int smithyPos = -1;
	  int adventurerPos = -1;
	  int salvagerPos = -1;
	  int great_hallPos = -1;
	  int i=0;
	  int numSmithies = 0;
	  int numAdventurers = 0;
	  int numSalvager = 0;
	  int numGreat_Hall = 0;
	  int smithywasplayed = 0;
	  int adventurewasplayed = 0;
	  int salvagerplayed = 0;
	  int great_hallplayed = 0;

	  while (!isGameOver(gsPre)) {
	    money = 0;
	    smithyPos = -1;
	    adventurerPos = -1;
	    salvagerPos = -1;
	    great_hallPos = -1;
	    
	    
	    for (i = 0; i < numHandCards(gsPre); i++) {
	      if (handCard(i, gsPre) == copper)
	    money++;
	      else if (handCard(i, gsPre) == silver)
	    money += 2;
	      else if (handCard(i, gsPre) == gold)
	    money += 3;
	      else if (handCard(i, gsPre) == smithy)
	    smithyPos = i;
	      else if (handCard(i, gsPre) == adventurer)
	    adventurerPos = i;
	      else if (handCard(i, gsPre) == salvager)
	    	 salvagerPos = i;
	      else if (handCard(i, gsPre) == great_hall)
	    	  great_hallPos = i;
	    }

	    fprintf(fp, "Player %d's turn\n", gsPre->whoseTurn);
	    printf("Player %d's turn\n", gsPre->whoseTurn);

	      if (smithyPos != -1 && smithywasplayed < 2) {
	    	  fprintf(fp, "smithy played from position %d\n", smithyPos);
	        printf("smithy played from position %d\n", smithyPos);
		        smithywasplayed += 1;

		// Get the current players turn and send in the adventurer card and test it.
			pp = whoseTurn(gsPre);
			// Test Smithy card
			numErrors += smittyTest(pp, smithy, smithyPos, -1, -1, -1, gsPre);
			 fprintf(fp, "smithy played.\n");
			printf("smithy played.\n");


	      } // end if (smithyPos != -1)



	    else  if (adventurerPos != -1 && adventurewasplayed < 2) {
	    	 fprintf(fp, "adventurer played from hand position %d\n", adventurerPos);
	        printf("adventurer played from hand position %d\n", adventurerPos);
		    adventurewasplayed = adventurewasplayed + 1;

		// Get the current players turn and send in the adventurer card and test it.
		pp = whoseTurn(gsPre);
		numErrors += adventurerTest(pp, adventurer, adventurerPos, -1, -1, -1, gsPre);

			      } // end if


	    else if (salvagerPos != -1 && salvagerplayed < 2) {
	    	fprintf(fp, "Salvager played from hand position %d\n", salvagerPos);
	   	   	 	  printf("Salvager played from hand position %d\n", salvagerPos);

	   	   	 	// Get the current players turn and send in the salvager card and test it.
	   	   	 	  pp = whoseTurn(gsPre);
	   	   	 	// Test salvager card
	   	   	    numErrors += salvagerTest(pp, salvager, salvagerPos, -1, -1, -1, gsPre);
	   	   	    salvagerplayed = salvagerplayed + 1;

	   	   	  }

	    else if (great_hallPos != -1 && great_hallplayed < 2) {
	    	fprintf(fp, "great_hall played from hand position %d\n", great_hallPos);
	   	   	 	  printf("great_hall played from hand position %d\n", great_hallPos);

	   	   	 	// Get the current players turn and send in the great_hall card and test it.
	   	   	 	  pp = whoseTurn(gsPre);
	   	   	 	// Test great_hall card
	   	   	    numErrors += testGreatHall(pp, great_hall, great_hallPos, -1, -1, -1, gsPre);
	   	   	    great_hallplayed = great_hallplayed + 1;

	   	   	  }
	    else 	{
	    	// If we do not have any of these cards in hand but we want to play others then
	    	// we will loop through the hand and play what we have.

	    	int rancarddpos = -1;
	    	int i =0;
	    	 for (i = 0; i < numHandCards(gsPre); i++) {
	    		      if (handCard(i, gsPre) == curse)
	    		    	  rancarddpos = i;
	    		      else if (handCard(i, gsPre) == council_room)
	    		    	  rancarddpos = i;
	    		      else if (handCard(i, gsPre) == feast)
	    		    	  rancarddpos = i;
	    		      else if (handCard(i, gsPre) == gardens)
	    		    	  rancarddpos = i;
	    		      else if (handCard(i, gsPre) == mine)
	    		    	  rancarddpos = i;
	    		      else if (handCard(i, gsPre) == remodel)
	    		    	  rancarddpos = i;
	    		      else if (handCard(i, gsPre) == village)
	    		    	  rancarddpos = i;
	    		      else if (handCard(i, gsPre) == baron)
	    		    	  rancarddpos = i;
	    		      else if (handCard(i, gsPre) == minion)
	    		    	  rancarddpos = i;
	    		      else if (handCard(i, gsPre) == tribute)
	    		    	  rancarddpos = i;
	    		      else if (handCard(i, gsPre) == ambassador)
	    		    	  rancarddpos = i;
	    		      else if (handCard(i, gsPre) == cutpurse)
	    		    	  rancarddpos = i;
	    		      else if (handCard(i, gsPre) == embargo)
	    		    	  rancarddpos = i;
	    		      else if (handCard(i, gsPre) == outpost)
	    		    	  rancarddpos = i;
	    		      else if (handCard(i, gsPre) == sea_hag)
	    		    	  rancarddpos = i;
	    		      else if (handCard(i, gsPre) == treasure_map)
	    		       	 rancarddpos = i;
	    		    }
	    	 pp = whoseTurn(gsPre);
	    	 //printHand(pp, gsPre);
	    	 int cardinmyhand = -1;
	    	 int random[11] = { council_room, feast, gardens, mine, village, baron, minion, tribute, ambassador, outpost, sea_hag};
	    	 int bool = 0;
	    	 if(rancarddpos >= -1){


	    	 int number = floor(Random() * ((11-0)+1));
	    	 rancarddpos = 1;
	    	 cardinmyhand = random[number];
	    	 bool = 1;
	    	  }

	    	// printf("hit me and card is!");
	    	 if(rancarddpos >= -1)
	    	 {

	    		 if (bool != 1){
	    			 cardinmyhand = handCard(rancarddpos,gsPre);
	    		 }

	    		 fprintf(fp, "Played a Randomly selected card to exercise the program further from position %d\n", rancarddpos);
	    	  printf("Played a Randomly selected card in hand from position %d\n", rancarddpos);

	    		// Get the current players turn and send in Randomly selected card in hand and test it.
	    		  pp = whoseTurn(gsPre);

	    		  // Play Randomly selected card in hand check for errors
	    		     if ( cardEffect((int)random, 0, 0, 0, gsPre, rancarddpos, 0) < 0 )
	    		  	   {
	    		    	 numErrors += 1;

	    	    	    }

	    	 }

	    		}

	            money = 0;
	      		i=0;
	      		while(i<numHandCards(gsPre)){
	      		  if (handCard(i, gsPre) == copper){
	      		    playCard(i, -1, -1, -1, gsPre);
	      		    money++;
	      		  }
	      		  else if (handCard(i, gsPre) == silver){
	      		    playCard(i, -1, -1, -1, gsPre);
	      		    money += 2;
	      		  }
	      		  else if (handCard(i, gsPre) == gold){
	      		    playCard(i, -1, -1, -1, gsPre);
	      		    money += 3;
	      		  }
	      		  i++;
	      		} // end while
	      		 if (money >= 8) {
	      			 	 	fprintf(fp, "%d bought province\n", gsPre->whoseTurn);
	      			        printf("%d bought province\n", gsPre->whoseTurn);
	      			        buyCard(province, gsPre);
	      			      }
	      			      else if ((money >= 6) && (numAdventurers < 4)) {
	      			    	fprintf(fp,"%d bought adventurer\n" , gsPre->whoseTurn);
	      			        printf("%d bought adventurer\n" , gsPre->whoseTurn);
	      			        buyCard(adventurer, gsPre);
	      			        numAdventurers++;
	      			      }

	      			      else if ((money >= 4) && (money < 5) && (numSmithies < 3)) {
	      			    	fprintf(fp,"%d bought smithy\n", gsPre->whoseTurn);
	      			  	    printf("%d bought smithy\n", gsPre->whoseTurn);
	      			  	    buyCard(smithy, gsPre);
	      			  	    numSmithies++;
	      			  	      }

	      			      else if ((money >= 4) && (numSalvager < 4)) {
	      			    	fprintf(fp,"%d bought salvager\n", gsPre->whoseTurn);
	      			     	printf("%d bought salvager\n", gsPre->whoseTurn);
	      			     	buyCard(salvager, gsPre);
	      			     	numSalvager++;
	      			      }
	      			      else if ((money >= 3) && (numGreat_Hall < 4)) {
	      			    	fprintf(fp,"%d bought great_hall\n", gsPre->whoseTurn);
	      			    	printf("%d bought great_hall\n", gsPre->whoseTurn);
	      			    	buyCard(great_hall, gsPre);
	      			    	numGreat_Hall++;
	      			    	 }
	      			      else if (money >= 6){
	      			       fprintf(fp,"%d bought gold\n", gsPre->whoseTurn);
	      			       printf("%d bought gold\n", gsPre->whoseTurn);
	      			       buyCard(gold, gsPre);
	      			          }
	      			      else if (money >= 3){
	      			    	fprintf(fp,"%d bought silver\n", gsPre->whoseTurn);
	      			        printf("%d bought silver\n", gsPre->whoseTurn);
	      				    buyCard(silver, gsPre);

	      			      }
	      		 	 	 fprintf(fp,"endTurn\n");
	      		 	 	 printf("endTurn\n");
	      			     endTurn(gsPre);
	      			   printGameSummary(*gsPre, fp, x, gsPre->numPlayers);


	  } // end of While

	  fprintf(fp, "Finished game.\n\n");
	  printf ("Finished game.\n\n");




	  fprintf(fp,"Number of players were: %d\n", gsPre->numPlayers);
	  printf("Number of players were: %d\n", gsPre->numPlayers);
	  // After each run we need to reset everything otherwise the
	    // whole thing crashes after a few hundred or so tests.

	  int winners[MAX_PLAYERS];
	  getWinners(winners, gsPre);
	  printScores(gsPre);


	  free(gsPre);
	  struct gameState *gsPre = malloc(sizeof(struct gameState));
	    setState(gsPre);
	    gsPre->hand[0][gsPre->handCount[0]] = -1;
	   	gsPre->hand[0][gsPre->handCount[0] + 1] = -1;

	  }




	  printf ("ALL TESTS RUN!\n");

	  free(gsPre);

	  if (numErrors == 0){
		  fprintf(fp,"There were no errors found in this tests!\n");
		  printf("There were no errors found in this tests!\n");
	  }
	  else if(numErrors > 0){
		  fprintf(fp,"There were %d errors in the tests run!\n",numErrors);
		  printf("There were %d errors in the tests run!\n",numErrors);
	  }
	  else{
		  fprintf(fp,"The error count was not 0 or more so the tests puked! \n");
		  printf("The error count was not 0 or more so the tests puked! \n");
	  }

	return 0;
}



